package com.ly.lypopupwindowdemo.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.ly.lypopupwindowdemo.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by liying on 2017/7/7.
 */

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

    private List<String> mDataSet;
    private List<Integer> mHeight;

    //构造器，接受数据集
    public MyAdapter(List<String> data){
        mDataSet = data;
    }

    /**
     * 为了制造流布局
     * @param parent
     * @param viewType
     * @return
     */
//    public MyAdapter(List<String> data){
//        mDataSet = data;
//        mHeight = new ArrayList<Integer>();
//        //随机获取一个mHeight值
//        for (int i = 0; i < mDataSet.size(); i++)
//        {
//            mHeight.add( (int) (100 + Math.random() * 300));
//        }
//    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //加载布局文件
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layout,parent,false);
        ViewHolder vh = new ViewHolder(v);
        return vh;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        //将数据填充到具体的view中
        holder.mTextView.setText(mDataSet.get(position));
//        //绑定数据的同时，修改每个ItemView的高度
//        ViewGroup.LayoutParams lp = holder.itemView.getLayoutParams();
//        lp.height = mHeight.get(position);
//        holder.itemView.setLayoutParams(lp);
    }

    @Override
    public int getItemCount() {
        return mDataSet.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        public TextView mTextView;
        public ViewHolder(View itemView) {
            super(itemView);
            //由于itemView是item的布局文件，我们需要的是里面的textView，因此利用itemView.findViewById获
            //取里面的textView实例，后面通过onBindViewHolder方法能直接填充数据到每一个textView了
            mTextView = (TextView) itemView.findViewById(R.id.num);
        }
    }

    /**
     * 移除数据
     * @param position
     */
    public void removeData(int position){
        mDataSet.remove(position);
        notifyItemRemoved(position);
    }

    /**
     * 增加数据
     * @param position
     */
    public void addData(int position){
        mDataSet.add(position,"Add One");
        notifyItemInserted(position);
    }
    /**
     * 更改某个位置的数据
     */
    public void changeData(int position){
        mDataSet.set(position,"Item has changed");
        notifyItemChanged(position);
    }


}